package be.kuleuven.thesis.pages;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import be.kuleuven.thesis.R;
import be.kuleuven.thesis.status.PlaylistGenerator;

public class CurrentPlaylistGroup extends ActivityGroup{
	public static final int PLAYLIST_ID = 0;
	public static final int FILTERS_ID =  1;
	
	public static CurrentPlaylistGroup group;
	private ArrayList<Integer> history;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.history = new ArrayList<Integer>();
		group = this;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		history.clear();
		loadMainActivity();
	}
	
	public void loadMainActivity() {
		loadActivity(PLAYLIST_ID);
	}
	
	public void loadActivity(int id) {
		if(!history.isEmpty() && history.get(history.size()-1) == id)
			return;
		if(!history.isEmpty() && history.get(history.size()-1)==FILTERS_ID && id == PLAYLIST_ID) {
			PlaylistGenerator.getInstance().applyFilter();
			Toast.makeText(getApplicationContext(), getResources().getString(R.string.changes_applied), Toast.LENGTH_SHORT).show();
			GenerecsTabActivity parentActivity;
		    parentActivity = (GenerecsTabActivity) this.getParent();
		    parentActivity.setOnTabChangedListener(null);
		}
		history.add(id);
		loadActivityDoSo(id);
	}
	
	private void loadActivityDoSo(int id) {
		View view = null;
		switch(id) {
		case PLAYLIST_ID:
			view = getLocalActivityManager().startActivity(id+"",
					new Intent(this,CurrentPlaylist.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
			break;
		case FILTERS_ID:
			view = getLocalActivityManager().startActivity(id+"",
					new Intent(this,SetRadioFilters.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
		}
		replaceView(view);
	}

	private void replaceView(View view) {
		setContentView(view);
	}

	public void back() {
		if(history.size() > 1) {
			if(history.get(history.size()-1)==FILTERS_ID) {
				PlaylistGenerator.getInstance().applyFilter();
				Toast.makeText(getApplicationContext(), getResources().getString(R.string.changes_applied), Toast.LENGTH_SHORT).show();
				GenerecsTabActivity parentActivity;
			    parentActivity = (GenerecsTabActivity) this.getParent();
			    parentActivity.setOnTabChangedListener(null);
			}
			history.remove(history.size()-1);
			loadActivityDoSo(history.get(history.size()-1));
			//setContentView(history.get(history.size()-1));
		}else {
			GenerecsTabActivity.tabActivity.back();
		}
	}

	@Override
	public void onBackPressed() {
		back();
		return;
	}
	
	public boolean onPrepareOptionsMenu(Menu menu) {
	    //super.onPrepareOptionsMenu(menu);
	    return getLocalActivityManager().getCurrentActivity()
	        .onCreateOptionsMenu(menu);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
	    //super.onCreateOptionsMenu(menu);
		System.out.println("GOOOOOOOOO");
	    return getLocalActivityManager().getCurrentActivity()
	        .onCreateOptionsMenu(menu);
	}

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		System.out.println("Daydream Nation");
	    //super.onMenuItemSelected(featureId, item);
	    return getLocalActivityManager().getCurrentActivity()
	        .onMenuItemSelected(featureId, item);
	}
}
